﻿using System.Web.Services;
using DadaLib;

namespace DadaWebService
{
    /// <summary>
    /// Summary description for GetSentenceService
    /// </summary>
    [WebService(Namespace = "http://dadaist.codeplex.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class GetSentenceService : WebService
    {

        /// <summary>
        /// Gets a generator from the application cache or creates one if 
        /// none is present.
        /// </summary>
        /// <returns></returns>
        public IGenerator GetGenerator()
        {
            if (Application["gen"] == null)
            {
                var dir = Server.MapPath(".");
                var gen = new Generator(dir + @"\App_Data\odyssey.txt");
                gen.Analize();
                Application["gen"] = gen;
            }
            return (IGenerator) Application["gen"];
        }

        /// <summary>
        /// Generates a sentence with the specified number of words.
        /// </summary>
        /// <param name="numberOfWords"></param>
        /// <returns></returns>
        [WebMethod]
        public string GetSentence(int numberOfWords)
        {
            var comp = new Composer(GetGenerator());
            return comp.ComposeSentence(numberOfWords);
        }

    }
}
